package thuai.ccf2013.query;

/**
 * The entity consist of the reference to a certain query, the name of keyword,
 * the start offset, the end offset, and knowledge base link. The knowledge base
 * link could be null.
 * 
 * @author WangYan
 * 
 */
public class Entity {

	public static final String NIL_KBLINK = "NIL";

	// the query context, including the original sentence
	private Query parentQuery = null;

	private String name = null;
	private int id = -1;
	private int startOffset = -1;
	private int endOffset = -1;
	private String kbLink = null;

	public Entity() {

	}

	public Entity(Query parent, int id, String name, int start, int end,
			String kbLink) {
		this.parentQuery = parent;
		this.id = id;
		this.startOffset = start;
		this.endOffset = end;
		this.kbLink = kbLink;
		this.name = name;

	}

	public boolean checkValidation() {

		assert (parentQuery != null);
		if (this.parentQuery == null) {
			return false;
		}

		String content = this.parentQuery.getContent();
		assert (content != null);
		assert (startOffset >= 0 && startOffset < endOffset) : this.toString();
		if (startOffset < 0 || startOffset >= endOffset) {
			return false;
		}
		assert (endOffset > 0 && endOffset <= content.length()) : this.toString() + " vs " + content.length();
		if (endOffset < 0 || endOffset > content.length()) {
			return false;
		}
		assert (name != null);
		if (this.name == null) {
			return false;
		}
//		assert (name.equals(content.substring(startOffset, endOffset))) : this.toString() + "\t" + content.substring(startOffset, endOffset);
		if (!name.equals(content.substring(startOffset, endOffset))) {
//			System.out.println(this.toString() + "\t" + content.substring(startOffset, endOffset));
			return false;
		}
		return true;
	}

	public Query getParentQuery() {
		return parentQuery;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

	public int getStartOffset() {
		return startOffset;
	}

	public int getEndOffset() {
		return endOffset;
	}

	public String getKbLink() {
		return kbLink;
	}

	public void setKbLink(String kbLink) {
		this.kbLink = kbLink;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.name);
		sb.append("\t[");
		sb.append(this.startOffset);
		sb.append(",");
		sb.append(this.endOffset);
		sb.append(")\t");
		sb.append(this.kbLink);
		return sb.toString();
	}

}
